package com.springboot.resttemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.springboot.resttemplate.common.CommonResult;
import com.springboot.resttemplate.entity.User;
import com.springboot.resttemplate.service.RetryService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/*
 *DATE:2025/7/28
 */
@SpringBootTest
@Slf4j
public class RestTemplateT {
    @Resource
    private RestTemplate restTemplate;
    @Test
    public void sendGet() throws JsonProcessingException {
        //第三方接口
        String url = "http://localhost:9090/user/getUserByUsername?username=张三&&address=上海";
        ResponseEntity<String> result = restTemplate.getForEntity(url, String .class);
        System.out.println(result.getStatusCode());
        System.out.println(result.getBody());
        String responseBody = result.getBody();
        //jackson 处理json的java工具
        ObjectMapper objectMapper = new ObjectMapper();
        CommonResult<User> commonResult = objectMapper.readValue(responseBody,new TypeReference<CommonResult<User>>() {});
        User data = commonResult.getData();
        log.info("user:{}",data);
        log.info("commonResult:"+commonResult);
        System.out.println(result.getHeaders());
    }

    @Test
    public void sendGet2(){
        String url = "http://localhost:9090/user/getUserByUsername?username={username}&address={address}";
        ResponseEntity<User> result = restTemplate.getForEntity(url,User.class,"张三","上海");
        System.out.println(result.getStatusCode());
        System.out.println(result.getBody());
        System.out.println(result.getHeaders());

    }

    @Resource
    private RetryService retryService;
    @Test
    public void testRetry() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = retryService.retryMethod();
        log.info("hhhh");
        String result = completableFuture.get();
        log.info("result:"+result);
    }
}
